package servlet.user;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import managers.EntityManagerAux;

import entities.User;
import exception.ValidationException;

/**
 * Servlet implementation class Modify. On utilise le servlet pour modifier 
 * l'information d'un utilisateur. Le servlet est en charge de valider l'information
 * fourni par le utilisateur.
 */
public class Modify extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Modify() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    @EJB EntityManagerAux mng;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int age_aux=0;
		//String user = (String)request.getAttribute("user2");
		//String user = request.getParameter("us");
		String user = (String)request.getSession().getAttribute("id");
		System.out.println("User is: "+user);
		
		String surname = request.getParameter("surname");
		String name = request.getParameter("name");
		String email = request.getParameter("mail");
		
		 boolean hasError = false;
	     String erreurs = "";

	     if (name.equals(null) || "".equals(name)) {
	            hasError = true;
	            erreurs += "<li>Nom incorrect</li>";
	        }
	        if (surname.equals(null) || "".equals(surname)) {
	            hasError = true;
	            erreurs += "<li>Prenom incorrect</li>";
	        }
	        if (email.equals(null) || "".equals(email)) {
	            hasError = true;
	            erreurs += "<li>Email incorrect</li>";
	        }
	        
	        if (hasError) {
	            throw new ValidationException(erreurs);
	        }

	        mng.updateUser(user,surname,name,email);
	        String goTo="close.jsp";
			request.getRequestDispatcher(goTo).forward(request, response);	        	   		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
